package ms.jasim.framework;

import ms.jacrim.pddl.PddlSolution.Functor;
import ms.jasim.framework.SolutionSimulator.SimulatedAction;
import ms.spm.IAppContext;

public class GoalFailEventType extends JEventType {

	public static final String GOAL = "Goal";

	public GoalFailEventType() {
		super();
		Parameters.add(new Parameter(GOAL, "Failed goal", ParameterType.Goal));
	}
	
	@Override
	public String getName() {
		return "Goal fail";
	}

	@Override
	public String getDescription() {
		return "Determine that the satisfaction of a given goal has been fail.";
	}

	@Override
	public boolean react(IAppContext context, PddlModel model, JEventInstance instance, SolutionSimulator simulator) {
		super.react(context, model, instance, simulator);
		PddlModel.Goal g = instance.getParamValue(GOAL);
		if (g != null) {
			for (SolutionSimulator.Agent ag: simulator.Agents) {
				SimulatedAction act = ag.getSimulatingAction(g);
				if (act != null && act.getAssociateAction().getFunctor() == Functor.SATISFIES) {
					model.addPostCommand(String.format("+!not (pr_satisfies %s %s)", ag.getName(), g.getName()));
					return true;
				}
			}
		}
		return false;
	}
}
